import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:rx_dart_app/model/global.dart';

class MoneyPage extends StatefulWidget {
  const MoneyPage({Key? key, this.content}) : super(key: key);

  final String? content;

  @override
  State<MoneyPage> createState() => _MoneyPageState();
}

class _MoneyPageState extends State<MoneyPage>
    with AutomaticKeepAliveClientMixin {
  late EasyRefreshController _controller;
  int _count = 10;

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
    _controller = EasyRefreshController(
      controlFinishRefresh: true,
      controlFinishLoad: true,
    );

    demoModel.init();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      appBar: AppBar(
        title: const Text("安全记账"),
      ),
      body: SafeArea(
        child: EasyRefresh(
            controller: _controller,
            header: const ClassicHeader(
                dragText: "下拉刷新",
                armedText: "松开手指即将刷新",
                readyText: "即将刷新",
                processingText: "加载中...",
                processedText: "加载完成"
                // messageText: "加载完成"
                ),
            footer: const ClassicFooter(noMoreText: "没有更多了"),
            onRefresh: () async {
              await Future.delayed(const Duration(seconds: 2));
              if (!mounted) {
                return;
              }
              setState(() {
                _count = 10;
              });
              _controller.finishRefresh();
              _controller.resetFooter();
            },
            onLoad: () async {
              await Future.delayed(const Duration(seconds: 2));
              if (!mounted) {
                return;
              }
              setState(() {
                _count += 5;
              });
              _controller.finishLoad(_count >= 20
                  ? IndicatorResult.noMore
                  : IndicatorResult.success);
            },
            child: ListView.builder(
              itemBuilder: (context, index) {
                return Card(
                  child: Container(
                    alignment: Alignment.center,
                    height: ScreenUtil().setHeight(80),
                    child: Text('${index + 1}'),
                  ),
                );
              },
              itemCount: _count,
            )),
      ),
      floatingActionButton: FloatingActionButton(
          child: const Icon(Icons.add),
          onPressed: () {
            // demoModel.init();
            demoModel.send();
          }),
    );
  }
}
